<?php
/* $Id$ */

class FileChecker {

	private $registeredFileFormatCallbacks = array (
			"image/png"	=> array (
						"createFunction" 	=> imagecreatefrompng,
						"extension"	 	=> ".png"
					   ),
			"image/jpeg"	=> array (
						"createFunction" 	=> imagecreatefromjpeg,
						"extension"		=> ".jpg"
					   ),		
			"image/gif"	=> array (
						"createFunction"	=> imagecreatefromgif,
						"extension"		=> ".gif"
					  )
	);

	private $fileObject;

	function FileChecker( $fileObject ) {
		$this->fileObject = $fileObject;
	}


	function getFileObject() {
		return $this->fileObject;
	}

	function getRegisteredFileFormatCallbacks() {
		return $this->registeredFileFormatCallbacks;
	}

	function isFileValidFormat() {
		
		$oldErrorLevel = error_reporting();
		$newErrorLevel = $oldErrorLevel;

		/* Als bereits def. zweier Potenz gegebene Bits loeschen */
		$newErrorLevel &= ~ E_WARNING /*(1 << E_WARNING)*/;
		$newErrorLevel &= ~ E_CORE_WARNING /*(1 << E_CORE_WARNING)*/;
		$newErrorLevel &= ~ E_USER_WARNING /*(1 << E_USER_WARNING)*/;

		/* Warnings ausschalten */		
		error_reporting( $newErrorLevel );

		$fileObject = $this->getFileObject();

		foreach ( $this->getRegisteredFileFormatCallbacks() as $key=>$value ) {
			/* Versuchen ueber GD in ein OffscreenBuffer zu schreiben */ 
			$img = call_user_func( $value["createFunction"], $fileObject["tmp_name"] );  
			if ( $img ) {
				/* Datei wurde validiert */
				error_reporting( $oldErrorLevel ); 
				return $key;
			};
 
		}

		error_reporting( $oldErrorLevel );
		return false;
	}

	function isFileAcceptedMimeFormat( $acceptedMimeFormat_Array ) {

		$validatedMime = $this->isFileValidFormat();

		if ($validatedMime !== false) {

			foreach( $acceptedMimeFormat_Array as $value ) {
				if ( $value == $validatedMime )
					return true;
			}

		};
		
		return false;

	} 


	function getMimeType() {
		return $this->isFileValidFormat();
	}

	function getExtensionByMimeType( $mimeType ) {
		return $this->registeredFileFormatCallbacks[$mimeType]["extension"];

	}

}



?>